package com.whfc.ms.api.emp.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.mqtt.MqttMessageParam;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.emp.entity.AppDeviceCardLog;
import com.whfc.emp.param.AddEmpDataParam;
import com.whfc.emp.param.EmpInfoParam;
import com.whfc.emp.service.AppInnerService;
import com.whfc.ms.api.emp.service.MsAppEmpInnerService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @ClasssName MsAppEmpInnerServiceImpl
 * @Description 内部应用
 * @Author hw
 * @Date 2021/1/5 14:14
 * @Version 1.0
 */
@Service
public class MsAppEmpInnerServiceImpl implements MsAppEmpInnerService {

    @DubboReference(interfaceClass = AppInnerService.class, version = "1.0.0",
            methods = {@Method(name = "batchAddEmpData", async = true, isReturn = false),
                    @Method(name = "updateEmpWorkType", async = true, isReturn = false)})
    private AppInnerService appInnerService;

    @Override
    public void buildEmpData(Integer empId, Integer deviceId, Date time, Double lat, Double lng) {
        appInnerService.buildEmpData(empId, deviceId, time, lat, lng);
    }

    @Override
    public void updateEmpInfo(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        Workbook workBook;
        try {
            workBook = WorkbookFactory.create(inputStream);
        } catch (Exception e) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "读取excel失败");
        }

        Sheet sheet = workBook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.rowIterator();
        List<EmpInfoParam> params = new ArrayList<>();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            // 身份证号
            Cell cell = row.getCell(0);
            if (cell == null) {
                continue;
            }
            String idcardNo = cell.getStringCellValue();
            if (StringUtils.isBlank(idcardNo)) {
                continue;
            }
            // 电话
            String phone = row.getCell(1).getStringCellValue();
            // 名族
            String nation = row.getCell(2).getStringCellValue();
            // 发证机关
            String org = row.getCell(3).getStringCellValue();
            // 住址
            String address = row.getCell(4).getStringCellValue();
            EmpInfoParam empInfoParam = new EmpInfoParam();
            empInfoParam.setAddress(address);
            empInfoParam.setIdcardNo(idcardNo);
            empInfoParam.setNation(nation);
            empInfoParam.setOrg(org);
            empInfoParam.setPhone(phone);
            params.add(empInfoParam);
        }
        appInnerService.updateEmpInfo(params);
    }

    @Override
    public void syncWorkTypeId(Integer deptId) {
        appInnerService.syncWorkTypeId(deptId);
    }

    @Override
    public void refreshPolygonWKT(Integer deptId) {
        appInnerService.refreshPolygonWKT(deptId);
    }

    @Override
    public void amendHelmetData(List<AppDeviceCardLog> list) {
        for (AppDeviceCardLog deviceCardLog : list) {
            deviceCardLog.setTime(DateUtil.addHours(deviceCardLog.getTime(), 8));
        }
        appInnerService.amendHelmetData(list);
    }

    @Override
    public void addEmpData(Date date, Integer empId, Integer deviceId, Double lat, Double lng) {
        appInnerService.addEmpData(date, empId, deviceId, lat, lng);
    }

    @Override
    public void batchAddEmpData(AddEmpDataParam param) {
        appInnerService.batchAddEmpData(param);
    }

    @Override
    public void cleanEmpAttendRecord(Date startTime, Date endTime) {
        appInnerService.cleanEmpAttendRecord(startTime, endTime);
    }

    @Override
    public void cleanFacefateRecord(Date startTime, Date endTime) {
        appInnerService.cleanFacegateRecord(startTime, endTime);
    }

    @Override
    public void sendMqttMessage(MqttMessageParam param) {
        appInnerService.sendMqttMessage(param);
    }

    @Override
    public void updateEmpWorkType() {
        appInnerService.updateEmpWorkType();
    }
}
